/*
day220
2020.4.4
*/ 
#include<map>
#include<vector>
#include<iostream>
using namespace std;

class Solution {
public:
    int numSubarraysWithSum(vector<int>& A, int S) {
    	int *arr=(int *)malloc(A.size()); 
		int size=A.size(),res=0;
		for(int i=1;i<size;++i)
		{
			A[i]+=A[i-1];
		}
		
		map<int,int> m;
		m[0]++;
		for(int i=0;i<size;++i)
		{
			res+=m[A[i]-S];
			m[A[i]]++;
		}
		
		return res;
    }
};

int main()
{
	Solution s;
	vector<int> A={1,0,1,0,1};
	int S=2;
	cout<<s.numSubarraysWithSum(A,S)<<endl;
	return 0;
}
